import { loginOut, getNowRouter } from '../libs/com'
import { Dialog } from 'vant'

const _this = window.thisWeb
const router = getNowRouter()
// 处理用户过期
const clearUserMge = res => {
	Dialog.alert({
		message: res.data
	}).then(() => {
	    if(router != "login"){
	    		loginOut()
	    }else{
	    		_this.$router.push({
				name: router
			})
	    }
	})
}

// 处理用户刷新本页面
const userSxPage = res => {
	Dialog.alert({
		message: res.data
	}).then(() => {
	    if(router != "login"){
	    		_this.$router.go(0)
	    }
	})
}

// 只是弹出框提示
const justAlert = res => {
	Dialog.alert({
		message: res.data
	}).then(() => {
		
	})
}

// 处理用户刷新本页面
const errorFomart = res => {
	if(res.status == "401") clearUserMge(res)
	else if(res.status == "500") userSxPage(res)
	else justAlert(res)
}

export const errorResponse = error => {
	let errorMessage = {}
		errorMessage.success = false
	let status = null
	if (error && error.response) {
		status = error.response.status
		errorMessage.status = status
		if(!error.response.data){
			errorMessage.error = 'web_message_error'
			errorMessage.data = null
		}else{
			errorMessage.error = error.response.data.error
			if(error.response.data.exception) errorMessage.data = error.response.data.exception
			if(error.response.data.message) errorMessage.data = error.response.data.message
			if(error.response.data.data) errorMessage.data = error.response.data.data
			if(error.response.data.error_description) errorMessage.data = error.response.data.error_description
		}
	}
	if(error && window.Cordova){
		status = error.code
		errorMessage.status = status
		if(error.response.error){
			errorMessage.error = error.response.error
		}else{
			errorMessage.error = 'android_message_error'
		}
		if(error.response.exception) errorMessage.data = error.response.exception
		if(error.response.message) errorMessage.data = error.response.message
		if(error.response.data) errorMessage.data = error.response.data
		if(error.response.error_description) errorMessage.data = error.response.error_description
	}
	switch (status) {
		case 400:
		if(!errorMessage.data) errorMessage.data = '请求错误.'
		break
		
		case 401:
		errorMessage.data = '当前会话已失效,请重新登录.'
		break
		
		case 403:
		if(!errorMessage.data) errorMessage.data = '拒绝访问.'
		break

		case 404:
		if(!errorMessage.data) errorMessage.data = '请求地址出错'
		break

		case 408:
		if(!errorMessage.data) errorMessage.data = '请求超时.'
		break

		case 500:
		if(!errorMessage.data) errorMessage.data = '响应超时,请刷新重试.'
		break

		case 501:
		if(!errorMessage.data) errorMessage.data = '服务未实现.'
		break

		case 502:
		if(!errorMessage.data) errorMessage.data = '网关错误.'
		break

		case 503:
		if(!errorMessage.data) errorMessage.data = '服务不可用.'
		break

		case 504:
		if(!errorMessage.data) errorMessage.data = '网关超时.'
		break

		case 505:
		if(!errorMessage.data) errorMessage.data = 'HTTP版本不受支持.'
		break
		
		case 800:
		if(!errorMessage.data) errorMessage.data = 'http请求方法错误.>'
		break
		
		case 801:
		if(!errorMessage.data) errorMessage.data = 'HTTP版本不受支持.>'
		break
		
		case 802:
		if(!errorMessage.data) errorMessage.data = 'Activity或Fragment已被销毁.>'
		break
		
		default:
		if(!errorMessage.data) errorMessage.data = '未知错误..'
		break
	}
	errorFomart(errorMessage)
	return errorMessage
}